<?php

class RouterAdmin
{
	public $path;
	public function __construct() {
		Registry::get("smarty")->template_dir = TEMPLATE_ADMIN;	
	}	
	
	public function delegate() 
	{
		self::getController($id);
		$registry = Registry::get("common_vars");		
		//print_r($registry);
        $file = $this->path."ControllerAdmin_".ucfirst($registry["controller_file"]);	
		//echo $file;
        if (is_readable($file) == false) {
        	die('404 Not Found');
        }
		
		/* Подключаем файл */
        include_once($file);		

		/* Дерево меню */
		if(($id == "5") || ($_GET['parent'] == 5)) { /* Конфигурация */
			$thisid = $_GET["id"] ? $_GET["id"] : $id;
			$ob_left_menu = new tree("configuration");
			$ob_left_menu->registry_parent_id($thisid);
			$left_menu = $ob_left_menu->getMenu(5, 0, $thisid);
		}
		elseif($id != "3" && $id != "4") { /* если не модуль и не темплейт */
			$thisid = $_GET["id"] ? $_GET["id"] : $id;
			$ob_left_menu = new tree($_GET["m"] ? $_GET["m"] : "content");
			$ob_left_menu->registry_parent_id($thisid);
			$left_menu = $ob_left_menu->getMenu(1, 0, $thisid);
		}
		
		/* TEMPLATES */
		Registry::get("smarty")->assign(array(
			'ACTION'      => $ob_admin_action,
			'HEADER'      => $registry['structure_name'],
			'LEFT_MENU'   => $left_menu,
			'LEFT_MENU_CATALOG' => $left_menu_catalog,
			'OUT'         => $ob_admin_out
		));
		exit(Registry::get("smarty")->fetch(TEMPLATE_ADMIN.'admin.tpl', Registry::get("cache_id")));
	}
	
	private function level_url_array(&$id) {
		switch($_GET["m"])
		{
			/* Контент */
			case "content":
			default:
				if(empty($_GET["id"])) {
					$id = 1;
				} else {
					$id = $_GET["id"];
				}
			break;
			
			/* Структура */	
			case "structure":$id = 2;break;	
			/* Модули */	
			case "modules":$id = 3;break;
			/* Темплейты */
			case "templates":
				$id = 4;
				$section = "templates";
			break;			
			/* Конфигурация */
			case "configuration":
				if(empty($_GET["id"])) {
					$id = 5;
				} else {
					$id = $_GET["id"];
				}
			break;
		}	
	}
	
	private function getController(&$id)
	{
        /* Получаем раздельные части URL */
        self::level_url_array($id);
		
		/* Получаем все данные модуля */
		$base_name = Registry::get("DB_NAME");	
		$r =& Registry::get("MDB")->query("SELECT 
			".DB_STRUCTURE.".ID as structure_id,
			".DB_STRUCTURE.".Name as structure_name,
			".DB_STRUCTURE.".Sys_name as structure_sys_name,
			".DB_STRUCTURE.".Parent as structure_parent_id,
			modules.Controller as controller_file,
			templates.Tpl as controller_template				
		FROM 
			".$base_name.".".DB_STRUCTURE.", 
			".$base_name.".".PRFX."modules,
			".$base_name.".".PRFX."templates
		WHERE 
			".DB_STRUCTURE.".Module = ".PRFX."modules.ID AND 
			".DB_STRUCTURE.".Template = ".PRFX."templates.ID AND 
			".DB_STRUCTURE.".ID = '".$id."'						
		LIMIT 1;");
	
		if (PEAR::isError($r)) {
			die($r->getMessage().' ::: '.$r->getUserInfo());
		}
        if($r->numRows()) {
			Registry::set("common_vars", $r->fetchRow(MDB2_FETCHMODE_ASSOC));
		}
	}
} 

?>